package com.yzcrm.erp.kms.controller.mobile;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.jfinal.log.Log;
import com.yzcrm.common.lucene.LuceneIndex;
import com.yzcrm.swagger.annotation.ActionApi;
import com.yzcrm.swagger.annotation.Api;
import com.yzcrm.swagger.annotation.Param;
import com.yzcrm.swagger.annotation.Params;
import com.yzcrm.utils.R;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;

import java.util.List;
import java.util.Map;

/**
 * @author jiang.xue
 * 全文搜索
 */
@Api(remark = "知识库手机端：全文搜索")
public class KmsMobileSearchController extends Controller{

    @Inject
    private LuceneIndex luceneIndex;

    /**
     * 全文搜索
     */
    @ActionApi(summary = "全文搜索", remark="全文搜索")
    @Params({
            @Param(name="content", remark="检索内容", dataType="String", defaultValue="", required=false)
    })
    public void search(@Para("content") String content) {

//        JSONObject jsonObject = basePageRequest.getJsonObject().fluentPut("type", CrmEnum.KMS_BBS.getType());
//        basePageRequest.setJsonObject(jsonObject);

//        String searchField1 = "title";
//        String searchField2 = "content";
//
//        Query query1 = new TermQuery(new Term(searchField1, content));
//        Query query2 = new TermQuery(new Term(searchField2, content));
//
//        BooleanQuery.Builder builder = new BooleanQuery.Builder();
//
//        builder.add(query1, BooleanClause.Occur.SHOULD);
//        builder.add(query2, BooleanClause.Occur.SHOULD);
//
//        BooleanQuery query = builder.build();


        List<Map> result = null;
        try {
            result = luceneIndex.search(content);
        } catch (Exception e) {
            e.printStackTrace();
            Log.getLog(KmsMobileSearchController.class).info("全文搜索异常：" + e.getMessage());
        }

        renderJson(R.ok().put("data", result));//        renderJson();
    }


}
